import dayjs from 'dayjs';
import { onMounted, onUnmounted, ref } from 'vue';

const dayMap = ['日', '一', '二', '三', '四', '五', '六'];

export const useTime = () => {
  let timer: any = null;
  const nowTime = ref('');

  const initTimer = () => {
    timer = setInterval(() => {
      getTime();
    }, 1000);
  };

  const getTime = () => {
    const ymd = dayjs().format('YYYY.MM.DD');
    const hms = dayjs().format('HH:mm:ss');
    const day = dayjs().get('day');

    nowTime.value = `${ymd} / 星期${dayMap[day]} / ${hms}`;
  };

  onMounted(() => {
    initTimer();
  });

  onUnmounted(() => {
    clearInterval(timer);
    timer = null;
  });

  return { nowTime };
};
